package com.wtwd.device.mqtentity;


import com.wtwd.device.constant.Constant;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.ByteObjectUtils;
import com.wtwd.device.util.StringUtils;
import lombok.*;

import java.io.Serializable;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class MessageBase implements Serializable {

    private static final long serialVersionUID = 396948278978631344L;
    private String deviceKey;

    private WTProtocol wtProtocol;

    private AppProtocol appProtocol;

    /**
     * 0 设备 1 app
     */
    private int type;

    /**
     * 得到服务器发送来源标记
     */
    @Builder.Default
    private String form = String.valueOf(StringUtils.ipToLong(Constant.LOCAL_IP));

    public static MessageBase deserialize(byte[] body) {
        return (MessageBase) ByteObjectUtils.toObject(body);
    }


    @Override
    public String toString() {
        return "MessageBase{" +
                "deviceKey='" + deviceKey + '\'' +
                ", wtProtocol=" + wtProtocol +
                ", appProtocol=" + appProtocol +
                ", type=" + type +
                ", form='" + form + '\'' +
                '}';
    }

    public byte[] serialize() {
        return ByteObjectUtils.toByteArray(this);
    }

}

